package com.copycoll;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ServerInfos {
	String state;
	String url;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setState(String state) {
		this.state = state;
	}
	public ServerInfos(){
		url="empty";
		state="OK";

			
	}
	
		public void getCodeRetour(String xmlRep) throws SAXException, IOException, ParserConfigurationException{
			
			ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//			Document doc = XMLfunctions.XMLfromString(xmlRep);
//	
//			int numResults = XMLfunctions.numResults(doc);
//	
//			if((numResults <= 0)){
//
//			}
//	
//			NodeList nodes = doc.getElementsByTagName("root");
		        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		        DocumentBuilder db = factory.newDocumentBuilder();
		        InputSource inStream = new InputSource();
		        inStream.setCharacterStream(new StringReader(xmlRep));
		        Document doc = db.parse(inStream);  

//		        String playcount = "empty";
		        NodeList nl = doc.getElementsByTagName("root");
//		        for(int i = 0; i < nl.getLength(); i++) {
//		            if (nl.item(i).getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
//		                 org.w3c.dom.Element nameElement = (org.w3c.dom.Element) nl.item(i);
//		                 playcount = nameElement.getFirstChild().getNodeValue().trim();
//		             }
//		        }
			for (int i = 0; i < nl.getLength(); i++) {							
				HashMap<String, String> map = new HashMap<String, String>();	
	
				Element e = (Element)nl.item(i);
				map.put("codeRet", XMLfunctions.getValue(e, "codeRet"));
				map.put("value",XMLfunctions.getValue(e, "value"));
				//map.put("Score", "Score: " + XMLfunctions.getValue(e, "score"));
				mylist.add(map);
				
				
			}
//			 this.url = playcount;
			 this.url = mylist.get(0).toString();

	
			 //= serverSource.setUrl(mylist.get(1).toString());
			
		}
	
	

}
